﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Farigola.Engine.SystemData.Entity.Type;

namespace Farigola.Engine.SystemData.Entity.inst
{
    /// <summary>
    /// Represent a folder, has child and has a folder parent, null for the root.
    /// </summary>
    public class EntInstFolder : EntInstBase
    {
        //=====================================================================
        #region Data members.

        /// <summary>
        /// the folder parent containing this entity.
        /// if null, its the root folder of the project.
        /// </summary>
        private EntInstFolder _folderParent;

        /// <summary>
        /// childs of the folder, list of entities
        /// can be types or instances entities.
        /// </summary>
        private List<EntityBase> _listEntityBase = new List<EntityBase>();

        #endregion

        //=====================================================================
        #region Constructors.

        //---------------------------------------------------------------------
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="systOrUser"></param>
        /// <param name="id"></param>
        /// <param name="name"></param>
        public EntInstFolder(EntityDef.TypeSystOrUser systOrUser, EntityId id, String name)
            : base(systOrUser, id, name)
        {            
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        public EntInstFolder(EntityId id, String name)
            : base(id, name)
        {            
        }
        #endregion

        //=====================================================================
        #region Get/Set methods.

        //---------------------------------------------------------------------
        /// <summary>
        /// return the folder parent (means the place in the tree) or null 
        ///for this entity.
        /// </summary>
        /// <returns></returns>
        public EntInstFolder GetParentFolder()
        {
            return _folderParent;
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// set the folder parent to this entity.
        /// </summary>
        /// <param name="parent"></param>
        public void SetParentFolder(EntInstFolder parent)
        {
            _folderParent = parent;
        }


        //---------------------------------------------------------------------
        /// <summary>
        /// Return list of all childs, user and system type.
        /// </summary>
        /// <returns></returns>
        public List<EntityBase> GetListChild()
        {
            return _listEntityBase;
        }

        #endregion

        //---------------------------------------------------------------------
        /// <summary>
        /// Return the count of all childs, user and system type.
        /// </summary>
        /// <returns></returns>
        public int ChildCount()
        {
            return _listEntityBase.Count;
        }

        //---------------------------------------------------------------------
        /// <summary>
        /// Find a child (scan only direct child) by the name.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public EntityBase FindChildByName(String name)
        {
		    if(name==null)
			    return null;
		    name = name.Trim();
		    if(name.Length==0)
			    return null;
		
		     foreach(EntityBase ent in _listEntityBase) {
		            if (ent.GetName().Equals(name,StringComparison.OrdinalIgnoreCase)) 
		                return ent;		        
		     }
		     return null; 		
	    }

        //---------------------------------------------------------------------
        /// <summary>
        /// Find a child (scan only direct child) by the id.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public EntityBase FindChildById(EntityId id)
        {
		    if(id==null)
			    return null;
		
		    if(!id.CheckInternal())
			    return null;
		
		     foreach(EntityBase ent in _listEntityBase) {
		            if (ent.GetId().IdAreEquals(id)) 
		                return ent;		        
		     }

		     return null;
	    }

        //---------------------------------------------------------------------
        /// <summary>
        /// Add a new child in the folder.
        ///  
        /// Check that the entity is not already present.
        /// Can't add a null child.
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool AddChild(EntityBase entity)
        {
            if (entity == null)
                return false;

            // check that the child is not already present
            if (_listEntityBase.Contains(entity))
                return false;

            // ok add the new child
            _listEntityBase.Add(entity);
            return true;
        }
    }
}
